<?php

class DefaultController extends Controller {
	
	public $m_str_title;
	
	public function actionIndex() {
	    $this->actionLogin();
	}
	/*
	 * Admin Login form 
	 */
	public function actionLogin() {
		if(isset(Yii::app()->user->isAdmin) && Yii::app()->user->isAdmin === 1) {
			if (Yii::app()->user->role == 1)
				$this->redirect(Yii::app()->getBaseUrl().'/admin/dashboard');
			else
				$this->redirect(Yii::app()->getBaseUrl().'/admin/datacard');
		}
		else {
			$model=new LoginForm('Admin');
	
			// collect user input data
			if(isset($_POST['LoginForm'])) {
				$model->attributes = $_POST['LoginForm'];
				if($model->validate()) {
				return true;
				}
				else {
					return false;
				}
			}
			// display the login form
			$this->m_str_title = 'Lake B2B::Admin-Login';
			$this->layout = 'login';
			$this->render('index',array('model'=>$model));
		}
	}
	
	/**
	 * This is the action to handle external exceptions.
	 */
	public function actionError(){
	
		if($error=Yii::app()->errorHandler->error)
		{
			if(Yii::app()->request->isAjaxRequest)
				echo $error['message'];
			else
				$this->renderPartial('error', $error);
		}
	}

	/**
	 * Logs out the current user and redirect to homepage.
	 */
	public function actionLogout() {
		Yii::app()->user->logout();
		$this->redirect(Yii::app()->getBaseUrl().'/admin');
	}
}